<template>
  <div id="app">
    <router-view />
  </div>
</template>


<script>
  import setting from '@/config/setting';
  export default {
    name: 'App',
    mounted() {
      // 统一监听所有通知消息

      //this.$socket.on('re_heartbeat', this.handReHeartbeat)
      this.$socket.on('connect', this.handConnect)
      this.$socket.on('notice', this.handleNotice)
      this.$socket.on('system', this.handleSystem)
      this.$socket.on('chat', this.handleChat)
    },
    beforeDestroy() {
      //this.$socket.off('re_heartbeat', this.handReHeartbeat)
      this.$socket.off('connect', this.handConnect)
      this.$socket.off('notice', this.handleNotice)
      this.$socket.off('system', this.handleSystem)
      this.$socket.off('chat', this.handleChat)
    },
    methods: {
      handReHeartbeat(data) {

        console.log('[心跳回复]', data)
      },
      handConnect(data) {
        let client_id = localStorage.getItem('client_id');
        let new_client_id = data.client_id;
        if (!client_id || (client_id != new_client_id)) {
          localStorage.setItem('client_id', new_client_id);
          console.log('[客户端id已更新]', new_client_id)
        }
        console.log('[连接信息]', data)
      },
      handleNotice(data) {
        console.log('[全局通知]', data)
        // 你可以用 this.$message、this.$notify、Element UI 弹窗等方式提示

      },
      handleSystem(data) {
        console.log('[系统消息]', data)
        if (data.event == 'logout') {
          this.$notify.error({
            title: "退出登录",
            message: '相同账号登录，发生异常！',
          });
          this.$store.dispatch('user/removeToken').then(() => {
            location.replace('/login');
          });
        }
        if (data.event == 'exp_logout' ) {
          const token = setting.takeToken();
          if(token == 'Bearer '+data.token){
            this.$notify.error({
              title: "退出登录",
              message: '令牌失效，重新登录！',
            });
            this.$store.dispatch('user/removeToken').then(() => {
              location.replace('/login');
            });
          }
        }
      },
      handleChat(data) {
        console.log('[聊天消息]', data)
      }
    }
  }
</script>

<style>
 /* @import "@/common/public.css"; */


</style>
